﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace IntelligentDecisioning.SharePoint.Definition.Fields
{
    /// <summary>
    /// This class encapsulates a SharePoint field of type "Number".
    /// The Number field type requires the number of decimal places to be set.
    /// If 0 decimal places is required use <code>SPNumberFormats.NoDecimal</code> and the field type will be be set to <code>SPFieldType.Integer</code>
    /// This prevents the need for an IntegerField class - are there any issues with this?
    /// </summary>
    public class NumberField: Field
    {
        public bool? UseCommas { get; set; }
        public SPNumberFormatTypes? DecimalPlaces { get; set; }
        public int? Max { get; set; }
        public int? Min { get; set; }


        public NumberField()
        {
            base.Type = SPFieldType.Number.ToString();
        }

        protected override void AddCustomFieldAttributes(System.Xml.XmlWriter xmlWriter)
        {
            base.AddCustomFieldAttributes(xmlWriter);

            if (UseCommas.HasValue && UseCommas.Value == true)
            {
                xmlWriter.WriteAttributeString(DefinitionConstants.USECOMMAS_ATTRIBUTE, DefinitionConstants.TRUE);
            }

            if (DecimalPlaces.HasValue)
            {
                // Don't set integer as there is no such type in UI
                //if (DecimalPlaces == SPNumberFormatTypes.NoDecimal)
                //{
                //    base.Type = SPFieldType.Integer.ToString();
                //}
                int i = (int)Enum.Parse(typeof(SPNumberFormatTypes), Enum.GetName(typeof(SPNumberFormatTypes), DecimalPlaces));
                xmlWriter.WriteAttributeString(DefinitionConstants.DECIMALS_ATTRIBUTE, i.ToString());
            }

            if (Max.HasValue)
            {
                xmlWriter.WriteAttributeString(DefinitionConstants.MAXVALUE_ATTRIBUTE, Max.Value.ToString());
            }

            if (Min.HasValue)
            {
                xmlWriter.WriteAttributeString(DefinitionConstants.MINVALUE_ATTRIBUTE, Min.Value.ToString());
            }
        }
    }
}
